/*
作者：江怀玉
日期：2014-06-10
功能：弹出编辑
解决：
-------------------------------------
依赖JS:
jquery,jquery.data
依赖CSS：
---------------------------------------------------------*/
//初始化控件
(function ($) {
    $.fn.dialog = function (options) {
        var _opt = !options ? {} : options;
        var $this = this;
        $(this).show();
        var h = _opt.height ? _opt.height : 550;
        var w = _opt.width ? _opt.width : 650;
        h = $(window).height() > h ? h : $(window).height()-10;
        w = $(window).width() > w ? w : $(window).width();
        //var w = $(window).width();
        $(this).height(h);
        $(this).width(w);
        fnCloseWindow();
        $(this).find(".main_data_dialog_content").height(h - 35);
        $(this).find(".main_data_dialog_content>iframe").height(h - 35 - 5);
        $(this).find("#main_data_dialog_footer").width(500);
        $(this).find("#main_data_dialog_footer").height(30);

        $(this).find(".main_data_dialog_footer>button").click(function () {
            switch ($(this).attr("id")) {
                case "btn_dialog_save":
                    fnWindowSaveData();
                    break;
                case "btn_dialog_save_and_return":
                    fnWindowSaveAndReturn();
                    break;
                case "btn_dialog_cancel":
                    fnCloseWindow();
                    break;
                case "btn_dialog_open":
                    fnOpenWindow(_EditDataURL);
                    break;
                case "btn_dialog_view":
                    fnOpenWindow(_EditDataURL + "&read=true");
                    break;
                default:
                    break;
            }
        });
        //数据保存
        function fnWindowSaveData() {
            var editwindow = document.getElementById("main_dialog_iframe");
            if (editwindow) {
                editwindow.contentWindow.Save();
            }
        }

        function fnWindowSaveAndReturn() {
            var editwindow = document.getElementById("main_dialog_iframe");
            if (editwindow) {
                editwindow.contentWindow.Save(fnCloseWindow);
                fnCloseWindow();
            }
        }
        //关闭窗口
        function fnCloseWindow() {
            $($this).animate({ left: (0 - $(window).width() - 5) + "px" });
            //$("#main_data_edit_iframe").attr("src", "");
        }
        //最小化窗口
        function fnMinWindow() {
            $($this).animate({ left: "-250px" });
        }

        ///-------------------------
        ///打开窗口
        ///-------------------------
        function fnOpenWindow(url) {
            $("#main_dialog_iframe").attr("src", url);
            var _left = $(window).width() / 2 - (650 / 2);
            var _top = $(window).height() / 2 - ($($this).height() / 2);
            $($this).animate({ top: _top + "px", left: _left + "px" });
        }


        ///-------------------------
        ///
        ///-------------------------
        function fnViewWindow(url) {
            $("#main_dialog_iframe").attr("src", url);
            $("#btn_dialog_save_and_return").hide();
            $($this).animate({ left: "-2px" });
        }


        ///------
        ///取消
        ///------------------------
        function fnCancel() {
            fnCloseWindow();
        }

        if (_opt.action == "open") {
            fnOpenWindow(_opt.url);
        }
    }
})(jQuery);


